/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vistas;

import Modelo.Canales;
import Modelo.Ciudades;
import Modelo.Clientes;
import Modelo.Funciones;
import Modelo.Servicios;
import javax.swing.DefaultListModel;

public class Agregar extends javax.swing.JInternalFrame {

    Object[][] clientes;
    DefaultListModel modeloListCanalesExtras = new DefaultListModel();
    DefaultListModel modeloListServiciosContratados = new DefaultListModel();

    public Agregar() {
        initComponents();
        this.desactivarCampos();
        //Obtengo las ciudades
        Ciudades ciu = new Ciudades();
        //Poblar canales
        for (Object[] ciudad : ciu.getCiudades()) {
            cbCiudad.addItem(ciudad[1]);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jmnombre = new javax.swing.JLabel();
        mciudad = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        mfono = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        addrut = new javax.swing.JLabel();
        txtrut = new javax.swing.JTextField();
        cbCiudad = new javax.swing.JComboBox();
        mmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        mdeuda = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        chbDeuda = new javax.swing.JCheckBox();
        seguiradd = new javax.swing.JButton();
        agregar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listCanalesExtras = new javax.swing.JList();
        cbCanales = new javax.swing.JComboBox();
        addCanal = new javax.swing.JButton();
        btnQuitarCanal = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        listServiciosContratados = new javax.swing.JList();
        cbPlanes = new javax.swing.JComboBox();
        lbPlan = new javax.swing.JLabel();
        cbServicios = new javax.swing.JComboBox();
        btnAddServicio = new javax.swing.JButton();
        btnQuitarServicio = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Agregar Cliente");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft New Tai Lue", 1, 12))); // NOI18N

        jmnombre.setText("Nombre:");

        mciudad.setText("Ciudad:");

        mfono.setText("Teléfono:");

        addrut.setText("Rut cliente:");

        mmail.setText("E-mail");

        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });

        mdeuda.setText("Deuda :");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mfono)
                            .addComponent(mmail))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtTelefono)
                                .addGap(195, 195, 195)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mciudad)
                            .addComponent(mdeuda))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chbDeuda))
                        .addGap(61, 61, 61))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(addrut)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtrut, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(194, 194, 194)
                                .addComponent(jLabel1))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jmnombre)
                                .addGap(18, 18, 18)
                                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 57, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addrut, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtrut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jmnombre)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel1))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(mfono)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(mciudad)
                            .addComponent(cbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mmail, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(chbDeuda)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(mdeuda))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        seguiradd.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        seguiradd.setText("Seguir agregando");
        seguiradd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seguiraddActionPerformed(evt);
            }
        });

        agregar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        agregar.setText("Agregar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Canales Extras", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N

        listCanalesExtras.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(listCanalesExtras);

        addCanal.setText("Agregar Canal");
        addCanal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCanalActionPerformed(evt);
            }
        });

        btnQuitarCanal.setText("Quitar");
        btnQuitarCanal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarCanalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnQuitarCanal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addCanal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbCanales, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(cbCanales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addCanal)
                        .addGap(34, 34, 34)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnQuitarCanal)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        cancelar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        cancelar.setText("Cancelar");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servicios contratados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N

        listServiciosContratados.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listServiciosContratados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listServiciosContratadosMouseClicked(evt);
            }
        });
        listServiciosContratados.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listServiciosContratadosValueChanged(evt);
            }
        });
        jScrollPane4.setViewportView(listServiciosContratados);

        cbPlanes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbPlanesItemStateChanged(evt);
            }
        });
        cbPlanes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPlanesActionPerformed(evt);
            }
        });

        lbPlan.setText("Plan");

        cbServicios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbServiciosActionPerformed(evt);
            }
        });

        btnAddServicio.setText("Agregar Servicio");
        btnAddServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddServicioActionPerformed(evt);
            }
        });

        btnQuitarServicio.setText("Quitar Servicio");
        btnQuitarServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarServicioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnAddServicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cbServicios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnQuitarServicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(lbPlan)
                        .addGap(71, 71, 71))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(cbPlanes, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(lbPlan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPlanes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(btnQuitarServicio)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGap(0, 34, Short.MAX_VALUE)
                        .addComponent(cbServicios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAddServicio))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(seguiradd, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(57, 57, 57)
                                .addComponent(agregar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(62, 62, 62)
                                .addComponent(cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 23, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(seguiradd)
                    .addComponent(agregar)
                    .addComponent(cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed
        String info[] = {txtrut.getText(), txtNombre.getText(), txtEmail.getText(), txtTelefono.getText()};
        int sw = 0;

        for (int i = 0; i < info.length; i++) {
            if (info[i].trim().length() == 0) {
                sw = 1;
            }
        }
        if (sw == 0) {
            String rut = info[0].trim();
            Clientes c = new Clientes();
            if (c.getCliente(rut) == null) {

                boolean deuda = chbDeuda.isSelected();
                String ciudad = String.valueOf(cbCiudad.getSelectedItem());

                Ciudades ciu = new Ciudades();

                int id_ciudad = (ciu.getIdCiudadByNombre(ciudad));

                c.addCliente(rut, info[1], info[3], info[2], deuda, id_ciudad);

                this.activarCampos();

                //poblar listCanales
                this.poblarCbCanales(rut);

                //poblar listCanalesExtras
                this.poblarListCanalesExtras(rut);

                //poblr ListServiciosContratados que tiene el clienet
                this.poblarListServiciosContratados(rut);

                //poblar servicios que no tiene
                this.poblarCbServicios(rut);

            } else {
                Funciones.mensaje("El cliente ya existe");

            }
        } else {
            Funciones.mensaje("Debe llenar todos los campos");

        }


    }//GEN-LAST:event_agregarActionPerformed

    private void btnQuitarCanalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarCanalActionPerformed
        if (listCanalesExtras.getSelectedIndex() > -1) {
            Modelo.Canales c = new Modelo.Canales();
            String rut = txtrut.getText();

            int id_canal = c.getIdCanalByNombre(String.valueOf(listCanalesExtras.getSelectedValue()));
            //Borro el canal al cliente
            c.deleteCanalExtra(rut, id_canal);
            //Agrego a la lista
            modeloListCanalesExtras.removeElementAt(listCanalesExtras.getSelectedIndex());

            //poblar CbtCanales
            this.poblarCbCanales(rut);

        } else {
            Funciones.mensaje("Debe seleccionar un canal extra que desea borrar");
        }
    }//GEN-LAST:event_btnQuitarCanalActionPerformed

    private void addCanalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCanalActionPerformed
        if (cbCanales.getSelectedIndex() > -1) {
            if (cbCanales.getSelectedIndex() > -1) {
                Canales c = new Canales();
                String rut = txtrut.getText();

                int id_canal = c.getIdCanalByNombre(String.valueOf(cbCanales.getSelectedItem()));
                //Añado el canal al cliente
                c.addCanalToCliente(rut, id_canal);
                //Agrego a la lista
                modeloListCanalesExtras.addElement(cbCanales.getSelectedItem());

                //poblar CbCanales
                this.poblarCbCanales(rut);
            } else {
                Funciones.mensaje("Debe seleccionar un canal para poder agregarlo");
            }
        }
    }//GEN-LAST:event_addCanalActionPerformed

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEmailActionPerformed

    private void btnQuitarServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarServicioActionPerformed
        // TODO add your handling code here:
        if (listServiciosContratados.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtrut.getText();

            int id_servicio = s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue()));
            //Borro el canal al cliente
            s.deleteServicioCliente(rut, id_servicio);
            //Agrego a la lista
            modeloListServiciosContratados.removeElementAt(listServiciosContratados.getSelectedIndex());
            cbPlanes.removeAllItems();
            //poblar CbServicios
            this.poblarCbServicios(rut);

        } else {
            Funciones.mensaje("Debe seleccionar un servicio para poder borrarlo");
        }
    }//GEN-LAST:event_btnQuitarServicioActionPerformed

    private void btnAddServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddServicioActionPerformed
        // TODO add your handling code here:
        if (cbServicios.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtrut.getText();

            int id_servicio = s.getIdServicioByNombre(String.valueOf(cbServicios.getSelectedItem()));
            //Añado el canal al cliente
            s.addServicioToCliente(rut, id_servicio);
            //Agrego a la lista
            modeloListServiciosContratados.addElement(cbServicios.getSelectedItem());

            //poblar CbServicios
            this.poblarCbServicios(rut);
        } else {
            Funciones.mensaje("Debe seleccionar un servicio para poder agregarlo");
        }
    }//GEN-LAST:event_btnAddServicioActionPerformed

    private void listServiciosContratadosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listServiciosContratadosValueChanged
        // TODO add your handling code here:
        if (listServiciosContratados.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtrut.getText();

            //obtengo los planes del servicio seleccionado
            this.poblarCbPlanes(rut, s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue())));

        }
    }//GEN-LAST:event_listServiciosContratadosValueChanged

    private void listServiciosContratadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listServiciosContratadosMouseClicked

    }//GEN-LAST:event_listServiciosContratadosMouseClicked

    private void cbServiciosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbServiciosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbServiciosActionPerformed

    private void seguiraddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seguiraddActionPerformed

        cbCanales.removeAllItems();
        modeloListServiciosContratados.removeAllElements();
        modeloListCanalesExtras.removeAllElements();
        this.desactivarCampos();
        txtrut.setText("");
        txtNombre.setText("");
        txtEmail.setText("");
        txtTelefono.setText("");
        chbDeuda.setSelected(false);
        cbPlanes.removeAllItems();
        cbServicios.removeAllItems();


    }//GEN-LAST:event_seguiraddActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelarActionPerformed

    private void cbPlanesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbPlanesItemStateChanged
        if (cbPlanes.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtrut.getText();
            int id_servicio = s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue()));
            int id_plan = s.getIdPlanByNombre(String.valueOf(cbPlanes.getSelectedItem()));

            //Guardo el plan seleccionado
            s.addPlanToCliente(rut, id_servicio, id_plan);
        }
    }//GEN-LAST:event_cbPlanesItemStateChanged

    private void cbPlanesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPlanesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbPlanesActionPerformed
    public void poblarCbCanales(String rut) {
        //Obtengo los canales
        Canales canales = new Canales();
        //Borro todos
        cbCanales.removeAllItems();
        //Poblar canales
        for (Object[] canal : canales.getCanalesNoTieneCliente(rut)) {
            cbCanales.addItem(canal[0]);
        }
    }

    public void poblarCbPlanes(String rut, int id_servicio) {
        Servicios ser = new Servicios();
        //Borro todos
        cbPlanes.removeAllItems();

        //Me fijo que plan tiene el cliente en ese servicio
        String plan_cliente = ser.getNombrePlanByClienteYServicio(rut, id_servicio);

        //Poblar planes
        for (Object[] plan : ser.getPlanesByServicio(id_servicio)) {
            cbPlanes.addItem(plan[0]);
        }

        //Selecciono el plan del cliente en dicho servicio
        cbPlanes.setSelectedItem(plan_cliente);

    }

    public void poblarListCanalesExtras(String rut) {
        Canales canales = new Canales();

        //borro si hay canales en la lista
        modeloListCanalesExtras.removeAllElements();

        //Agrego los canales extras que tiene el cliente
        for (Object[] canalExtra : canales.getCanalesByCliente(rut)) {
            modeloListCanalesExtras.addElement(canalExtra[0]);
        }
        listCanalesExtras.setModel(modeloListCanalesExtras);
    }

    public void poblarListServiciosContratados(String rut) {
        //poblar lista Servicios
        Servicios ser = new Servicios();

        //Borro items
        modeloListServiciosContratados.removeAllElements();

        for (Object[] servicio : ser.getServiciosByCliente(rut)) {
            modeloListServiciosContratados.addElement(servicio[0]);
        }
        listServiciosContratados.setModel(modeloListServiciosContratados);

        //Marco la primera opcion
        listServiciosContratados.setSelectedIndex(0);
    }

    public void poblarCbServicios(String rut) {
        Servicios ser = new Servicios();
        //Boro si tiene
        cbServicios.removeAllItems();
        //Poblar servicios
        for (Object[] servicio : ser.getServicioNoTieneCliente(rut)) {
            cbServicios.addItem(servicio[0]);
        }
    }

    public void desactivarCampos() {

        listServiciosContratados.setEnabled(false);
        listCanalesExtras.setEnabled(false);
        cbCanales.setEnabled(false);
        addCanal.setEnabled(false);
        btnQuitarCanal.setEnabled(false);
        seguiradd.setEnabled(false);
        txtrut.setEnabled(true);
        txtNombre.setEnabled(true);
        txtTelefono.setEnabled(true);
        cbCiudad.setEnabled(true);
        txtEmail.setEnabled(true);
        chbDeuda.setEnabled(true);
        agregar.setEnabled(true);
        btnQuitarServicio.setEnabled(false);
        btnAddServicio.setEnabled(false);
        cbServicios.setEnabled(false);
        cbPlanes.setEnabled(false);

    }

    public void activarCampos() {

        listServiciosContratados.setEnabled(true);
        listCanalesExtras.setEnabled(true);
        cbCanales.setEnabled(true);
        addCanal.setEnabled(true);
        btnQuitarCanal.setEnabled(true);
        agregar.setEnabled(false);
        seguiradd.setEnabled(true);

        txtrut.setEnabled(false);
        txtNombre.setEnabled(false);
        txtTelefono.setEnabled(false);
        cbCiudad.setEnabled(false);
        txtEmail.setEnabled(false);
        chbDeuda.setEnabled(false);

        btnQuitarServicio.setEnabled(true);
        btnAddServicio.setEnabled(true);
        cbServicios.setEnabled(true);
        cbPlanes.setEnabled(true);

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCanal;
    private javax.swing.JLabel addrut;
    private javax.swing.JButton agregar;
    private javax.swing.JButton btnAddServicio;
    private javax.swing.JButton btnQuitarCanal;
    private javax.swing.JButton btnQuitarServicio;
    private javax.swing.JButton cancelar;
    private javax.swing.JComboBox cbCanales;
    private javax.swing.JComboBox cbCiudad;
    private javax.swing.JComboBox cbPlanes;
    private javax.swing.JComboBox cbServicios;
    private javax.swing.JCheckBox chbDeuda;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel jmnombre;
    private javax.swing.JLabel lbPlan;
    private javax.swing.JList listCanalesExtras;
    private javax.swing.JList listServiciosContratados;
    private javax.swing.JLabel mciudad;
    private javax.swing.JLabel mdeuda;
    private javax.swing.JLabel mfono;
    private javax.swing.JLabel mmail;
    private javax.swing.JButton seguiradd;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtrut;
    // End of variables declaration//GEN-END:variables
}
